package bsi.vn.FirstApp;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

@SuppressWarnings("deprecation")
public class TabContainActivity extends TabActivity implements OnTouchListener{
	
	TabHost tabHost;
	TabWidget tabWidget;
	LinearLayout layout;
	float xDown;
	int currentTab;
	Context context = TabContainActivity.this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab);
		tabHost = getTabHost();
		tabWidget = tabHost.getTabWidget();
		layout = (LinearLayout) findViewById(R.id.tab);
		
		TabSpec tabSpec1 = tabHost.newTabSpec("all");
		tabSpec1.setIndicator("ALL");
		Intent intent1 = new Intent(context, MainActivity.class);
		tabSpec1.setContent(intent1);
		
		TabSpec tabSpec2 = tabHost.newTabSpec("ticket");
		tabSpec2.setIndicator("TICKET");
		Intent intent2 = new Intent(context, TicketSearchActivity.class);
		tabSpec2.setContent(intent2);
		
		tabHost.addTab(tabSpec1);
		tabHost.addTab(tabSpec2);
		
		layout.setOnTouchListener(this);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.atmsearch, menu);
		return true;
		
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float x = event.getX();
		
		
		float xUp = 0;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			currentTab = tabHost.getCurrentTab();
			xDown = event.getX();
//			Log.d("test", "down = " + xDown);

			break;
		case MotionEvent.ACTION_UP:
			xUp = x;

			if (xDown < xUp) {
//				Log.d("test", xDown + " < " + x);
//				Log.d("test", currentTab+" : back action");
				if (currentTab > 0) {
					tabHost.setCurrentTab(currentTab - 1);
//					Log.d("test", "back tab");
				}
			}
			if (xDown > xUp) {
//				Log.d("test", currentTab+": next action");
				if (currentTab < 1) {
					tabHost.setCurrentTab(currentTab + 1);
//					Log.d("test", "next tab");
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			// Log.d("test", "Move: "+x);
			break;

		default:
			break;
		}
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		Toast.makeText(context, "menu item has clicked", Toast.LENGTH_LONG).show();
		return super.onMenuItemSelected(featureId, item);
	}


}
